package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IQuestAnswer;
import com.wbao.dianniu.listener.IQuestAnswerListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class QuestAnswerManager implements IQuestAnswer {
    private List<IQuestAnswerListener> mListener = new ArrayList<IQuestAnswerListener>();

    private Context mContext;
    public static QuestAnswerManager instance;

    public static QuestAnswerManager getInstance(Context ctx) {
        if (null == instance) {
            instance = new QuestAnswerManager(ctx);
        }
        return instance;
    }


    private QuestAnswerManager(Context ctx) {
        mContext = ctx;
    }

    @Override
    public void addResponse(int accountId, int questId, String content, String noticeJson, String answerImgs, int pid, Integer anonymous) {
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACTION_ACCOUNT_QUEST_ANSWER_ADD)
                    .addArgs("accountId", accountId)
                    .addArgs("questId", questId)
                    .addArgs("content", content)
                    .addArgs("noticeJson", noticeJson)
                    .addArgs("answerImgs", answerImgs)
                    .addArgs("pid", pid)
                    .addArgs("anonymous", anonymous);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                QuestAnswerListData dat = JsonUtil.toObject(obj, QuestAnswerListData.class);
                // 通知界面
                if (null != mListener) {
                    for (int i = 0; i < mListener.size(); i++) {
                        mListener.get(i).onQuestAnswerSuccess(dat);
                    }
                }
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener) {
                    for (int i = 0; i < mListener.size(); i++) {
                        mListener.get(i).onQuestAnswerFailure(errorCode, msg);
                    }
                }
            }
        });
    }

    @Override
    public boolean addQuestAnswerListener(IQuestAnswerListener listener) {
        if (null != mListener) {
            mListener.add(listener);
        }
        return mListener != null;
    }

    @Override
    public boolean removeQuestAnswerListener(IQuestAnswerListener listener) {
        if (null != mListener) {
            mListener.remove(listener);
        }
        return true;
    }

}
